import api from '@/api';

export default {
  namespace: 'customerRule',

  state: {
    customerRuleStatus: {},
    customerGroupRules: {
      list: [],
      pagination: {},
    },
    customerRules: {
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      let res = yield call(api.customerRule.group.query, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCustomerGroupRules',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
    },
    *fetchRuleById({ payload }, { call, put }) {
      return yield call(api.customerRule.group.queryRuleById, payload);
    },
    *create({ payload }, { call }) {
      return yield call(api.customerRule.group.create, payload)
    },
    *audit({ payload }, { call }) {
      return yield call(api.customerRule.group.audit, payload)
    },
    *delete({ payload }, { call }) {
      return yield call(api.customerRule.group.delete, payload)
    },
    *enable({ payload }, { call }) {
      return yield call(api.customerRule.group.enable, payload)
    },
    *disable({ payload }, { call }) {
      return yield call(api.customerRule.group.disable, payload)
    },
    *fetchCustomerRuleStatus({ payload }, { call, put }) {
      const res = yield call(api.customerRule.group.queryStatus, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCustomerRuleStatus',
          payload: res.data,
        });
      }
    },
    *fetchCustomerRules({ payload }, { call, put }) {
      return yield call(api.customerRule.queryItem, payload);
      // const res = yield call(api.customerRule.queryItem, payload);
      // if (res.errorCode === 0) {
      //   yield put({
      //     type: 'saveCustomerRules',
      //     payload: {
      //       list: res.data,
      //     },
      //   });
      // }
    },
    *fetchGroupRuleDetail({ payload }, { call, put }) {
      return yield call(api.customerRule.group.queryRuleDetail, payload);
    },
    *createRule({ payload }, { call }) {
      return yield call(api.customerRule.create, payload)
    },
    *auditRule({ payload }, { call }) {
      return yield call(api.customerRule.audit, payload)
    },
    *deleteRule({ payload }, { call }) {
      return yield call(api.customerRule.delete, payload)
    },
    *fetchRuleDetail({ payload }, { call, put }) {
      return yield call(api.customerRule.queryRuleDetail, payload);
    },
  },

  reducers: {
    saveCustomerGroupRules(state, { payload }) {
      return {
        ...state,
        customerGroupRules: payload,
      };
    },
    saveCustomerRuleStatus(state, { payload }) {
      return {
        ...state,
        customerRuleStatus: payload
      };
    },
    saveCustomerRules(state, { payload }) {
      return {
        ...state,
        customerRules: payload
      };
    },
  },
};
